//一个请求可能被多个对象处理，但是运行时只有一个接受者
//目前似乎这种模式只是一种数据结构
enum class RequestType
{
	REQ_HANDLER1,
	REQ_HANDLER2,
	REQ_HANDLER3
};

class Request
{
	string description;
	RequestType reqType;
public:
	Request(const string& desc, RequestType type):description(desc),reqType(type){}
	RequestType getType() const{return type;}
	const string& getDescription() const {return description;}
};

class ChainHandler
{
	ChainHandler* nextChain; //指向自己，多态的链表
	void sendRequestToNextHandler(const Request& req)
	{
		if(nextChain != nullptr)
		{
			nextChain->handle(req);
		}
	}
protected:
	virtual bool canHandleRequest(const Request& req) = 0;
	virtual void processRequest(const Request& req) = 0;
public:
	ChainHandler(){nextChain = nullptr;}
	void sendNextChain(ChainHandler* next){nextChain = next}
	void handle(const Request& req)
	{
		//可以处理即处理，不可以处理交给下一个链表节点处理
		if(canHandleRequest(req))
			processRequest(req);
		else
			sendRequestToNextHandler(req);
	}
}

class Handler1:public ChainHandler
{
protected:
	bool canHandleRequest(const& req) override
	{
		return req.getReqType() == RequestType::REQ_HANDLER1;
	}
	void processRequest(const Request& req) override
	{
		cout << "handler 1 is handling request" << req.getDescription() << endl;
	}
}

class Handler2:public ChainHandler
{
protected:
	bool canHandleRequest(const& req) override
	{
		return req.getReqType() == RequestType::REQ_HANDLER2;
	}
	void processRequest(const Request& req) override
	{
		cout << "handler 2 is handling request" << req.getDescription() << endl;
	}
}

//handler 3
//...

int main()
{
	Handler1 h1;
	Handler2 h2;
	Handler3 h3;
	h1.sendNextChain(&h2);
	h2.sendNextChain(&h3);

	Request req("process task ...",RequestType::REQ_HANDLER3);
	h1.handle(req);
	return 0;
}